package com.thinta.wind.ui.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.lzy.ninegrid.ImageInfo
import com.lzy.ninegrid.preview.NineGridViewClickAdapter
import com.thinta.wind.R
import com.thinta.wind.databinding.HotComItemViewBinding
import com.thinta.wind.entity.HotDataItemBean
import com.thinta.wind.interfaces.OnItemClickListener
import java.util.*

/*********************************************************
 * 版权信息：2021/7/5 15:37北京振中电子技术有限公司版权所有<p>
 * 创建作者：Wind<p>
 * 创建日期：2021/7/5 15:37<p>
 * *******************************************************<p>
 * 更新历史：    作者      日期      更新摘要<p>
 * <p>
 * *******************************************************<p>
 * 类功能说明：<p>
 */
class HotComAdapter : PagingDataAdapter<HotDataItemBean, RecyclerView.ViewHolder>(differCallback) {

    private lateinit var itemClickListener : OnItemClickListener

    fun setOnItemListener(itemClickListener: OnItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as HotViewHolder).bindItemData(holder.itemView.context, getItem(position), itemClickListener)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = HotViewHolder(parent)

    companion object{

        private val differCallback = object : DiffUtil.ItemCallback<HotDataItemBean>(){

            override fun areItemsTheSame(
                oldItem: HotDataItemBean,
                newItem: HotDataItemBean
            ): Boolean = oldItem.weiboId == newItem.weiboId

            override fun areContentsTheSame(
                oldItem: HotDataItemBean,
                newItem: HotDataItemBean
            ): Boolean = oldItem == newItem

        }
    }
}

class HotViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(
        R.layout.hot_com_item_view, parent, false
    )
){

    private lateinit var mBinding : HotComItemViewBinding

    fun bindItemData(context : Context, data: HotDataItemBean?, itemClickListener: OnItemClickListener){
        mBinding = initViewBinding(itemView) as HotComItemViewBinding
        mBinding.commentCount.text = data?.commentNum.toString()
        when (data?.userInfo?.isvertify) {
            1 -> {
                mBinding.verifyIcon.setImageResource(R.mipmap.home_vertify)
            }
            2 -> {
                mBinding.verifyIcon.setImageResource(R.mipmap.home_vertify2)
            }
            else -> {
                mBinding.verifyIcon.visibility = View.GONE
            }
        }
        if(data?.picurl?.size != 0){
            val imageInfo = ArrayList<ImageInfo>()
            for (imageDetail in data!!.picurl) {
                val info = ImageInfo()
                info.setThumbnailUrl(imageDetail)
                info.setBigImageUrl(imageDetail)
                imageInfo.add(info)
            }
            mBinding.nineGrid.visibility = View.VISIBLE
            mBinding.nineGrid.setAdapter(NineGridViewClickAdapter(context, imageInfo))
        }
        mBinding.item = data
        mBinding.root.setOnClickListener {
            itemClickListener.onItemClick(layoutPosition, it)
        }
    }
}


private fun initViewBinding(itemView: View) : ViewDataBinding? =
    if (null == DataBindingUtil.getBinding(itemView)) {
        DataBindingUtil.bind(itemView)
    } else {
        DataBindingUtil.getBinding(itemView)
    }


